package com.huyn.baseframework.dynamicload;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.view.ContextThemeWrapper;

import com.ryg.dynamicload.internal.DLPluginPackage;

/**
 * Created by huyaonan on 16/10/26.
 */
public class DLProxyContext extends ContextThemeWrapper {

    private DLPluginPackage mPlugin;
    private Theme mTheme;

    public DLProxyContext(Context baseContext, DLPluginPackage pluginPackage) {
        attachBaseContext(baseContext);
        mPlugin = pluginPackage;
        if(mPlugin != null && mPlugin.resources != null) {
            mTheme = mPlugin.resources.newTheme();
            mTheme.setTo(baseContext.getTheme());
        }
    }

    @Override
    public Resources getResources() {
        return (mPlugin == null || mPlugin.resources == null) ? super.getResources() : mPlugin.resources;
    }

    @Override
    public ClassLoader getClassLoader() {
        return (mPlugin == null || mPlugin.classLoader == null) ? super.getClassLoader() : mPlugin.classLoader;
    }

    @Override
    public AssetManager getAssets() {
        return (mPlugin == null || mPlugin.assetManager == null) ? super.getAssets() : mPlugin.assetManager;
    }

    @Override
    public Theme getTheme() {
        return (mPlugin == null || mTheme == null) ? super.getTheme() : mTheme;//mPlugin.resources.newTheme();
    }

}
